﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Sbook.Data
{
    public class NewsController:SqlDataProvider
    {
        #region[News_GetByAll]
        public List<NewsInfo> News_GetByAll()
        {
            List<Data.NewsInfo> list = new List<Data.NewsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_News_GetByAll", GetConnection()))
            {
                Data.NewsInfo obj = new Data.NewsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.NewsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion

        
        #region[NewsInfo_GetById]
        public List<NewsInfo> News_GetById(string Id)
        {
            List<Data.NewsInfo> list = new List<Data.NewsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_News_GetById", GetConnection()))
            {
                Data.NewsInfo obj = new Data.NewsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@nID", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.NewsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        
        #region[NewsInfo_GetByTop]
        public List<NewsInfo> News_GetByTop(string Top, string Where, string Order)
        {
            List<Data.NewsInfo> list = new List<Data.NewsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("[sp_News_GetByTop]", GetConnection()))
            {
                Data.NewsInfo obj = new Data.NewsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.NewsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[News_Insert]
        public bool News_Insert(NewsInfo news)
        {
            using (SqlCommand cmd = new SqlCommand("sp_News_Insert", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@nTitle", news.nTitle));
                cmd.Parameters.Add(new SqlParameter("@nDescription", news.nDescription));
                cmd.Parameters.Add(new SqlParameter("@nContent", news.nContent));
                cmd.Parameters.Add(new SqlParameter("@nStatus", news.nStatus));
                cmd.Parameters.Add(new SqlParameter("@GroupID",news.groupID));
                cmd.Parameters.Add(new SqlParameter("@Image", news.Image));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region[News_Update]
        public bool News_Update(NewsInfo news)
        {
            using (SqlCommand cmd = new SqlCommand("sp_News_Update", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@nId",news.nID));
                cmd.Parameters.Add(new SqlParameter("@nTitle", news.nTitle));
                cmd.Parameters.Add(new SqlParameter("@nDescription", news.nDescription));
                cmd.Parameters.Add(new SqlParameter("@nContent", news.nContent));
                cmd.Parameters.Add(new SqlParameter("@nStatus", news.nStatus));
                cmd.Parameters.Add(new SqlParameter("@GroupID", news.groupID));
                cmd.Parameters.Add(new SqlParameter("@Image", news.Image));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region[News_UpdateStatus]
        public bool News_UpdateStatus(string Id, string status)
        {
            using (SqlCommand cmd = new SqlCommand("sp_News_UpdateStatus", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@nId", Id));
                cmd.Parameters.Add(new SqlParameter("@nStatus", status));
                
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        public bool News_Delete(string Id)
        {
            using (SqlCommand cmd = new SqlCommand("sp_News_DeleteById", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@nID",Id));
                cmd.ExecuteNonQuery();
            }
            return true;
        }

        #region[News_Paging]
        public List<NewsInfo> News_Paging(string CurentPage, string PageSize)
        {
            List<Data.NewsInfo> list = new List<Data.NewsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_News_Paging", GetConnection()))
            {
                Data.NewsInfo obj = new Data.NewsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.NewsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion

        public int News_GetCount()
        {
            int total = 0;
            using (SqlCommand cmd = new SqlCommand("sp_News_GetCount", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                total = (int)cmd.ExecuteScalar();
            }
            return total;
        }
    }
}